package reduce.fz.com.reduce.activity;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.alibaba.fastjson.TypeReference;
import com.example.qingshengzheng.material.Text;

import de.greenrobot.event.EventBus;
import reduce.fz.com.reduce.R;
import reduce.fz.com.reduce.abstracts.AbResponseListener;
import reduce.fz.com.reduce.bean.ServiceException;
import reduce.fz.com.reduce.bean.User;
import reduce.fz.com.reduce.bean.Video;
import reduce.fz.com.reduce.bean.WitnessCard;
import reduce.fz.com.reduce.bean.result.Result;
import reduce.fz.com.reduce.custom.LoadingDialog;
import reduce.fz.com.reduce.event.WitnessCardEvent;
import reduce.fz.com.reduce.service.TeamService;
import reduce.fz.com.reduce.service.WitnessService;
import reduce.fz.com.reduce.service.preference.UserPreference;
import reduce.fz.com.reduce.util.HttpUtils;
import reduce.fz.com.reduce.util.ServiceBuilder;
import reduce.fz.com.reduce.util.base.ReduceApplication;
import reduce.fz.com.reduce.util.oss.Oss;

public class VideoNextActivity extends BaseToolBarActivity implements View.OnClickListener {
    private EditText mVideoDesc;
    private TextView mTvComplete;
    private String videoPath;
    private TeamService teamService = ServiceBuilder.builderTeamService(null);
    private LoadingDialog dialog = null;
    private Oss oss = Oss.instance();
    private Video video = null;
    private Long teamid;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_video_next);
        videoPath = getIntent().getStringExtra("videopath");
        teamid=getIntent().getLongExtra("teamid",0);
        initWidget();
    }

    @Override
    public void initWidget() {
        mVideoDesc = (EditText) findViewById(R.id.video_desc);
        mTvComplete = (TextView) findViewById(R.id.tv_complete);
        mTvComplete.setOnClickListener(this);
    }

    @Override
    public void initListener() {
    }

    public void submitFile() {
        video=new Video(videoPath);
        dialog=new LoadingDialog(this,"上传视频中!");
        dialog.show();
        oss.uploadVideo(video, new Oss.OssVideoResponse() {
            @Override
            public void onSuccess(Video video1) {
                video=video1;
                complete();
            }

            @Override
            public void onProgress(int complete, int total) {

            }

            @Override
            public void onFailed(Video video) {
                toastMsg("上传视频失败");
                return;
            }
        });

    }


    public void complete() {
        dialog.setMsg("提交信息中!");
        WitnessCard card = WitnessCard.videoCard(video.url, mVideoDesc.getText().toString());
        card.teamid=teamid;
        User user= UserPreference.getUser();
        card.user=user;
        teamService.pubWitness(card, new AbResponseListener<WitnessCard>(new TypeReference<Result<WitnessCard>>() {
        }) {
            @Override
            public void error(ServiceException e) {
                dialog.dismiss();

            }

            @Override
            public void responseInfo(Result<WitnessCard> data) {
                dialog.dismiss();
                EventBus.getDefault().post(new WitnessCardEvent(data.getData()));
                ReduceApplication.getInstance().finishActivity(VideoRecoderActivity.class);
                ReduceApplication.getInstance().finishActivity(VideoNextActivity.class);



            }
        });

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_complete:
                submitFile();
                break;
            default:
                break;
        }

    }
}
